<template>
  <div class="dashboard-container">
    <div v-loading="loading" class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <tree-tools @addDepts="addDepts" :is-root="true" :tree-node="company" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <tree-tools
            @addDepts="addDepts"
            @delDepts="getDepartments"
            slot-scope="{ data }"
            :tree-node="data"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹层组件 -->
    <AddDept
      ref="addDept"
      :treeNode="node"
      :show-dialog.sync="showDialog"
      @addDepts="getDepartments"
    ></AddDept>
  </div>
</template>

<script>
import treeTools from "./components/tree-tools";
import { getDepartments } from "@/api/departments";
import { tranListToTreeDate } from "@/utils";
import AddDept from "./components/add-dept";
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: "name",
        children: "children"
      },
      company: {},
      showDialog: false, // 显示弹窗
      node: null, // 用于存放点击的节点
      loading: false
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    // 读取页面数据
    async getDepartments() {
      this.loading = true;
      const result = await getDepartments();
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeDate(result.depts, "");
      this.loading = false;
    },
    // 显示弹层
    addDepts(node) {
      this.showDialog = true; // 显示弹窗
      this.node = node; // 获取到点击的节点
    },
    editDepts(node) {
      this.showDialog = true; // 显示弹窗
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    }
  }
};
</script>

<style scoped>
.tree-card {
  padding: 10px 120px;
  font-size: 14px;
}
</style>
